/**
 * @file InetAddress.cpp
 * @author your name (you@domain.com)
 * @brief
 * @version 0.1
 * @date 2022-04-05
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "../../include/network/InetAddress.h"
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>

namespace wd
{

InetAddress::InetAddress(short port)
{
    ::memset(&addr_, 0, sizeof(addr_));
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    addr_.sin_addr.s_addr = INADDR_ANY;
}

InetAddress::InetAddress(const char *pIp, short port)
{
    ::memset(&addr_, 0, sizeof(addr_));
    addr_.sin_family = AF_INET;
    addr_.sin_port = htons(port);
    addr_.sin_addr.s_addr = inet_addr(pIp);
}

InetAddress::InetAddress(const struct sockaddr_in &addr)
    : addr_(addr) {}

const struct sockaddr_in *InetAddress::getSockAddrPtr() const
{
    return &addr_;
}

std::string InetAddress::ip() const
{
    return std::string(inet_ntoa(addr_.sin_addr));
}

unsigned short InetAddress::port() const
{
    return ntohs(addr_.sin_port);
}

} // end of namespace wd
